package tp.java.model;

import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

public class TPCalculette extends Activity implements Observer{

	Calculette cal;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		cal = new Calculette();
		this.cal.addObserver(this);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		final EditText et = (EditText) findViewById(R.id.editText1);
		
		
		Button empiler = (Button) findViewById(R.id.pushButton);
		empiler.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v){
				try {
					cal.enter(Integer.parseInt(et.getText().toString()));
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (CalculetteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		Button mul = (Button) findViewById(R.id.mulButton);

		mul.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v){
				try {
					cal.mul();
				} catch (CalculetteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		Button add = (Button) findViewById(R.id.addButton);

		add.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v){
				try {
					cal.add();
				} catch (CalculetteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		Button sub = (Button) findViewById(R.id.subButton);

		sub.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v){
				try {
					cal.sub();
				} catch (CalculetteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		Button div = (Button) findViewById(R.id.divButton);

		div.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v){
				try {
					cal.div();
				} catch (CalculetteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		Button clear = (Button) findViewById(R.id.clearButton);
		clear.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v){
				try {
					cal.clear();
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		TextView t = (TextView) findViewById(R.id.etaPile);
		t.setText(cal.toString());
		
		ProgressBar jauge = (ProgressBar) findViewById(R.id.jauge);
		jauge.setProgress(cal.size());

	}
}
